/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.customer;

import com.alibaba.fastjson.JSONArray;
import net.dgg.dggcloud.core.idwork.KeyWorker;
import net.dgg.dggcloud.core.util.DateUtil;
import net.dgg.dggcloud.core.util.MapToBeanUtils;
import net.dgg.dggcloud.core.util.NJCodeUtil;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.constant.CustomerConstant;
import net.dgg.dggcloud.service.jrcrm.constant.CustomerRecordConstant;
import net.dgg.dggcloud.service.jrcrm.dao.customer.CustomerDao;
import net.dgg.dggcloud.service.jrcrm.entity.customer.Contact;
import net.dgg.dggcloud.service.jrcrm.entity.customer.Customer;
import net.dgg.dggcloud.service.jrcrm.entity.customer.CustomerForm;
import net.dgg.dggcloud.service.jrcrm.entity.customer.CustomerRecord;
import net.dgg.dggcloud.service.jrcrm.service.cms.CustomerRecordService;
import org.apache.commons.lang.math.NumberUtils;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户表单管理业务处理层
 * @ClassName: CustomerFormService
 * @Author: 罗金
 * @Date: 2018年5月29 14:34:05
 * @Version V1.0
 */

@Service
public class CustomerFormService extends BaseService {

    @Resource
    private NJCodeUtil njCodeUtil;
    @Autowired
    protected KeyWorker keyWorker;

    /**
     * 保存客户信息
     * @param sb
     */
    public Long updateCustomer(CustomerForm sb,Map requestParamMap) {
        Query query = repository.getSession().createQuery("From CustomerForm where phoneBak ="+requestParamMap.get("customerPhone")+"");
        if(query.list().size()>0) {
            sb = (CustomerForm) query.list().get(0);
        }
        if(sb==null) {
            sb = new CustomerForm();
            sb.setCreateTime(new Date());
            sb.setId(keyWorker.nextId());
        }
        if (null != requestParamMap.get("customerName")) {//名称
            sb.setName(String.valueOf(requestParamMap.get("customerName")));
        }
        if (null != requestParamMap.get("age")&&NumberUtils.isNumber(requestParamMap.get("age")+"")) {//名称
            sb.setAge(NumberUtils.toInt(requestParamMap.get("age")+""));
        }
        if (null != requestParamMap.get("customerPhone")) {//电话
            sb.setPhone(String.valueOf(requestParamMap.get("customerPhone")));
            sb.setPhoneBak(sb.getPhone());
            sb.setPhone(njCodeUtil.encrypt(sb.getPhone()));
        }
        if (null != requestParamMap.get("money")&&NumberUtils.isNumber(requestParamMap.get("money")+"")) {//金额
            sb.setMoney(Long.parseLong(requestParamMap.get("money").toString()));
        }
        if (null != requestParamMap.get("birthday")) {//生日
            sb.setBirthday(DateUtil.stringToDate(requestParamMap.get("birthday").toString()+" 00:00:00","yyyy-MM-dd HH:mm:ss"));
        }
        if (null != requestParamMap.get("isHouse")) {//房子
            sb.setIsHouse(Integer.parseInt(requestParamMap.get("isHouse").toString()));
        }
        if (null != requestParamMap.get("isCar")) {//车
            sb.setIsCar(Integer.parseInt(requestParamMap.get("isCar").toString()));
        }
        if (null != requestParamMap.get("isFund")) {//公积金
            sb.setIsFund(Integer.parseInt(requestParamMap.get("isFund").toString()));
        }
        if (null != requestParamMap.get("isGuaranteeSlip")) { //保单
            sb.setIsGuaranteeSlip(Integer.parseInt(requestParamMap.get("isGuaranteeSlip").toString()));
        }
        if (null != requestParamMap.get("isCreditCard")) {
            sb.setIsCreditCard(Integer.parseInt(requestParamMap.get("isCreditCard").toString()));//信用卡
        }
        if (null != requestParamMap.get("isSocialSecurity")) {//社保
            sb.setIsSocialSecurity(Integer.parseInt(requestParamMap.get("isSocialSecurity").toString()));
        }
        if (null != requestParamMap.get("isOther")&&null != requestParamMap.get("isOtherValue")) {//芝麻 微粒贷
            sb.setIsOther(Integer.parseInt(requestParamMap.get("isOther").toString()));
            sb.setOtherValue(NumberUtils.toLong(requestParamMap.get("isOtherValue").toString()));
        }
        if (null != requestParamMap.get("isBuilding")) {//商品房
            sb.setIsBuilding(Integer.parseInt(requestParamMap.get("isBuilding").toString()));
        }
        if (null != requestParamMap.get("isShop")) {//商铺
            sb.setIsShop(Integer.parseInt(requestParamMap.get("isShop").toString()));
        }
        if (null != requestParamMap.get("isOffice")) {//写字楼
            sb.setIsOffice(Integer.parseInt(requestParamMap.get("isOffice").toString()));
        }
        if (null != requestParamMap.get("isOtherValue")&&NumberUtils.isNumber(requestParamMap.get("isOtherValue")+"")) {//   分数
            sb.setOtherValue(Long.parseLong(requestParamMap.get("isOtherValue").toString()));
        }
        this.repository.saveOrUpdate(sb);
        return 1L;
    }
};
